<template>
  <div class="login-container">
    <!-- 头像 -->
    <div class="heardPho">
      <img
        src="	http://likede2-admin.itheima.net/img/logo.595745bd.png"
        alt=""
      >
    </div>
    <el-form
      ref="loginRef"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      label-position="left"
    >
      <!-- 用户名 -->
      <el-form-item prop="loginName">
        <span class="svg-container">
          <!-- <svg-icon icon-class="el-icon-s-unfold" /> -->
          <i class="el-icon-mobile-phone" />
        </span>
        <el-input
          ref="loginName"
          v-model="loginForm.loginName"
          placeholder="请输入用户名"
          type="text"
          tabindex="1"
        />
      </el-form-item>
      <!-- 密码框 -->
      <el-form-item prop="password">
        <span class="svg-container">
          <!-- <svg-icon icon-class="password" /> -->
          <i class="el-icon-goods" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          tabindex="2"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>
      <!-- 验证码 -->
      <div class="yanzengma">
        <el-form-item prop="code">
          <span class="svg-container">
            <i class="el-icon-edit-outline" />
          </span>
          <el-input
            ref="code"
            v-model="loginForm.code"
            placeholder="请输入验证码"
            name="username"
            type="text"
            tabindex="3"
            auto-complete="on"
          />
          <div class="container" @click="newCode">
            <img :src="imgSrcUrl" alt="">
          </div>
        </el-form-item>
      </div>
      <!-- 登录 -->
      <el-button
        class="loginBtn"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        :loading="loading"
        @click.native.prevent="handleLogin"
      >登录</el-button>
    </el-form>
  </div>
</template>

<script>
// import { code } from '@/api'
import { Message } from 'element-ui'
export default {
  name: 'Login',
  data() {
    return {
      loading: false,
      imgSrcUrl: '',
      loginForm: {
        loginName: 'admin',
        password: 'admin',
        code: ''
      },
      num: '',
      num2: '',
      newloginForm: {},
      // 手机号校验
      loginRules: {
        loginName: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          {
            min: 4,
            max: 16,
            message: '密码长度只能是6~16位字符',
            trigger: 'blur'
          }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      },
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  async created() {
    // 验证码图片
    this.codeImg()
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 验证码的请求
    async codeImg() {
      // 验证码图片
      this.num = Math.ceil(Math.random() * 100)
      this.num2 = Math.random() * 10
      this.imgSrcUrl =
        'http://likede2-admin.itheima.net/likede/api/user-service/user/imageCode/' + this.num2
      console.log('this.imgSrcUrl', this.imgSrcUrl)
    },
    // 更新验证码
    newCode() {
      this.codeImg()
    },
    // 点击登录执行的方法
    // 判断输入框是否输入
    // 进行兜底校验
    handleLogin() {
      if (
        this.loginForm.loginName === '' ||
        this.loginForm.password === '' ||
        this.loginForm.code === ''
      ) { return Message.error('请将登录信息填写完整') }
      this.newloginForm = {
        ...this.loginForm,
        clientToken: this.num2,
        loginType: 0
      }
      this.$refs.loginRef.validate(async(valid) => {
        if (valid) {
          try {
            this.loading = true
            const res = await this.$store.dispatch(
              'user/fetchLogin',
              this.newloginForm
            )
            console.log(res)
            // 跳转到首页
            this.$router.replace('/')
            this.loading = false
          } catch (error) {
            this.loading = false
          }
        }
      })
      console.log(this.num)
    }
  }
}
</script>

<style lang="scss">
// 全局生效的样式
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #68b0fe;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #999;
      height: 47px;
      caret-color: #999;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    position: relative;
    border: 1px solid #dcdfe6;
    background: rgba(255, 255, 255, 0.7); // 输入登录表单的背景色
    border-radius: 5px;
    margin-top: -10px;
    margin-bottom: 30px;
    height: 50px;
    // display: flex;
  }

  .el-form-item__error {
    // color: #fff;
    padding-top: 0px;
  }
}
</style>

<style lang="scss" scoped>
// 只对当前组件生效的样式
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

// 头像
.heardPho {
  position: absolute;
  width: 96px;
  height: 96px;
  top: 120px;
  left: 50%;
  margin-left: -45px;
  z-index: 1;
  // background-color: #2e50e1;
}
.heardPho img {
  width: 100%;
}
.login-container {
  position: relative;
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background-image: url("http://likede2-admin.itheima.net/img/background.be4fae7d.png"); // 设置背景图片
  // background-position: center; // 将图片位置设置为充满整个屏幕
  background-repeat: no-repeat;
  background-size: cover;
  .login-form {
    position: absolute;
    position: relative;
    width: 468px;
    height: 350px;
    top: 50%;
    left: 50%;
    margin-top: 173px;
    margin-left: -234px;
    padding: 76px 35px 0;
    background: rgb(252, 251, 251);
    // -webkit-box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    border-radius: 10px;
  }
  // 验证码
  .container {
    position: absolute;
    left: 270px;
    top: -0px;
    width: 126px;
  }
  .container img {
    width: 100%;
  }
  // 登录按钮
  .loginBtn {
    margin-top: -3px;
    background: linear-gradient(262deg, #2e50e1, #6878f0);
    height: 45px;
    width: 100%;
    line-height: 20px;
    font-size: 16px;
    opacity: 0.91;
    border-radius: 8px;
    color: #fff;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
